<template>
    <el-dialog v-dialogDrag :title="!dataForm.pkNotice ? '新增' : '修改'" :close-on-click-modal="false"
        :before-close="closeDailog" :visible.sync="visible" width="40%">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
            <el-form-item label="店铺主键" prop="pkShop">
                <ec-mall-select v-model="dataForm.pkShop" placeholder="请选择店铺" ></ec-mall-select>
            </el-form-item>
            <el-form-item label="公告标题" prop="noticeTitle">
                <el-input v-model="dataForm.noticeTitle" placeholder="公告标题" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="公告内容" prop="noticeContent">
                <el-input v-model="dataForm.noticeContent" type="textarea" :autosize="{ minRows: 4}" placeholder="公告内容" maxlength="500" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="公告描述" prop="noticeDesc">
                <el-input v-model="dataForm.noticeDesc" placeholder="公告描述" maxlength="100" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="公告状态" prop="noticeStatus">
                <ec-dict-select dictType="enable_status" defaultIndex="0" v-model="dataForm.noticeStatus" placeholder="公告状态" ></ec-dict-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog()">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as noticeService from '@/api/mall/notice'

    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkShop: '',
                    pkNotice: 0,
                    noticeTitle: '',
                    noticeContent: '',
                    noticeDesc: '',
                    noticeStatus: '',
                    revision: '',
                },
                dataRule: {
                    pkShop: [{
                        required: true,
                        message: '店铺主键不能为空',
                        trigger: 'blur'
                    }],
                    noticeTitle: [{
                        required: true,
                        message: '公告标题不能为空',
                        trigger: 'blur'
                    }],
                    noticeContent: [{
                        required: true,
                        message: '公告内容不能为空',
                        trigger: 'blur'
                    }],
                    noticeDesc: [{
                        required: false,
                        message: '公告描述不能为空',
                        trigger: 'blur'
                    }],
                    noticeStatus: [{
                        required: true,
                        message: '公告状态',
                        trigger: 'blur'
                    }],
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkNotice = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkNotice) {
                        noticeService.getNoticeInfo(this.dataForm.pkNotice).then(data => {
                            this.dataForm.pkShop = data.pkShop
                            this.dataForm.noticeTitle = data.noticeTitle
                            this.dataForm.noticeContent = data.noticeContent
                            this.dataForm.noticeDesc = data.noticeDesc
                            this.dataForm.noticeStatus = data.noticeStatus
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkShop': this.dataForm.pkShop,
                            'pkNotice': this.dataForm.pkNotice || undefined,
                            'noticeTitle': this.dataForm.noticeTitle,
                            'noticeContent': this.dataForm.noticeContent,
                            'noticeDesc': this.dataForm.noticeDesc,
                            'noticeStatus': this.dataForm.noticeStatus,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.pkNotice ? noticeService.addNotice(params) : noticeService
                            .updateNotice(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            }
        }
    }
</script>